package org.jeecg.modules.yw.service;

import javax.annotation.Resource;

import org.jeecg.modules.yw.entity.DataStatistic;
import org.jeecg.modules.yw.mapper.DataStatisticMapper;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class DataStatisticService extends ServiceImpl<DataStatisticMapper, DataStatistic>  {
	@Resource 
	private DataStatisticMapper dataStatisMapper;
	/**
	 * 统计数量
	 * @param userId
	 * @param userType
	 * @param orderId
	 * @param type
	 * @param sl
	 * @return
	 */
	public int tjSl(String userId, Integer userType, String orderId, Integer type, String sl) {
		DataStatistic obj = dataStatisMapper.getSlExist(userId, orderId, type);
		if (obj == null) {
			obj = new DataStatistic();
			obj.setId(CommonUtils.getID());
			obj.setUserId(userId);
			obj.setUserType(userType);
			obj.setOrderId(orderId);
			obj.setType(type);
			obj.setSl(sl);
			obj.setTjDate(DateUtils.getCurrentTimeDateStr());
			dataStatisMapper.insert(obj);
		} else {
			String num = obj.getSl();
			String newnum = "0";
			Double d = Double.parseDouble(num);
			if (sl!=null && !sl.equals("0")) {
				d =  d + Double.parseDouble(sl);
				newnum = d.toString();
			} else {
				newnum = String.valueOf((Integer.parseInt(num) + 1));
			}
			obj.setSl(newnum);
			dataStatisMapper.updateById(obj);
			
		}
		return 0;
	}
	
	/**
	 * 统计数量
	 * @param userId
	 * @param userType
	 * @param orderId
	 * @param type
	 * @param sl
	 * @return
	 */
	public int insertTjsl(String userId, Integer userType, String orderId, Integer type, String sl) {
		DataStatistic obj =  new DataStatistic();
		obj.setId(CommonUtils.getID());
		obj.setUserId(userId);
		obj.setUserType(userType);
		obj.setOrderId(orderId);
		obj.setType(type);
		obj.setSl("1");
		obj.setTjDate(DateUtils.getCurrentTimeDateStr());
		dataStatisMapper.insert(obj);
		return 0;
	}
}
